function MarkerPicker(xpoint,ypoint){

	//默认可选参数
	var dconfig={
		"element":"",
		"width":500,
		"height":410,
		"isCenter":true,
		"isCityControl":true,
		"isFullScreen":true
	};
	var temp=this;
	//尺寸限制
	if(dconfig.width<500){
		dconfig.width=500;
	};
	if(dconfig.height<410){
		dconfig.height=410;
	};
	var time=new Date().getTime();
	var amapmap;
	var tempx;
	var tempy;
	var pannel=document.createElement("div");
	$(pannel).attr('id','MapAmap'+time);
	pannel.style.cssText="background:#FFF;display:inline-block;";
	pannel.innerHTML = ''+
					   '<div id="con'+time+'" style="width:'+(dconfig.width)+'px;height:'+(dconfig.height)+'px;position:relative;overflow:hidden;font-size:12px;">'+
							//菜单栏
							'<div style="height:28px;">'+
								'<div style="width:11px;height:27px;float:left;background:url(/Public/Js/map/images/tit_left.jpg);"></div>'+
								'<div id="main'+time+'" style="width:'+(dconfig.width-22)+'px;height:27px;float:left;background:url(/Public/Js/map/images/tit_main.jpg);overflow:hidden;">'+
									'<div id="btnClose'+time+'" style="width:14px;height:28px;float:right;background:url(/Public/Js/map/images/close.gif) no-repeat center center;cursor:pointer;"></div>'+
									'<div style="width:232px;line-height:28px;float:left;color:#0B7ECE;">1.选择位置&nbsp;&gt;&nbsp;2.标注位置&nbsp;&gt;&nbsp;3.保存位置</div>'+																									
									'<div id="nosave'+time+'" style="width:39px;line-height:28px;float:left;color:#f00;display:none;">未保存</div>'+
								'</div>'+
								'<div style="width:11px;height:27px;float:left;background:url(/Public/Js/map/images/tit_right.jpg);"></div>'+																	
							'</div>'+
							//地图容器
							'<div id="bo'+time+'" style="height:'+(dconfig.height-80)+'px;border:1px solid #999;border-bottom:0px none;">'+
								'<div id="amapmap" style="height:'+(dconfig.height-90)+'px;padding:5px;"></div>'+
							'</div>'+
							//底部
							'<div style="height:38px;padding-top:12px;background:url(/Public/Js/map/images/bottom.jpg);border:1px solid #999;border-top:0px none;position:relative;">'+
								'<div id="pointxy'+time+'" style="width:200px;line-height:28px;position:absolute;top:12px;left:10px;text-align:left;"></div>'+								
								'<div id="save'+time+'" style="width:106px;line-height:28px;position:absolute;top:12px;right:149px;text-align:center;cursor:pointer;background:url(/Public/Js/map/images/btn_marker.png)">保存</div>'+
								'<div id="cancel'+time+'" style="width:106px;line-height:28px;position:absolute;top:12px;right:30px;text-align:center;cursor:pointer;background:url(/Public/Js/map/images/btn_marker.png)">取消</div>'+
								'<div id="resize'+time+'" style="width:12px;height:12px;position:absolute;bottom:0px;right:0px;cursor:se-resize;background:url(/Public/Js/map/images/resize.gif);"></div>'+
							'</div>'+
							//修复IE6下下拉列表的bug
							'<iframe id="ifra'+time+'" style="width:'+(dconfig.width+1)+'px;height:'+(dconfig.height-2)+'px;position:absolute;top:-2px;left:-2px;z-index:-1;"></iframe>'+
							'</div><style>.amap-toolbar, .amap-scalecontrol, .amap-overviewcontrol{z-index:100;}</style>';
	$("body").append(pannel);
	setCenter();
	px = $('#'+ypoint).val();
	px = px?px:$('#'+ypoint).text();
	
	py = $('#'+xpoint).val();
	py = py?py:$('#'+xpoint).text();
	amapMap(px,py);
	//关闭窗口
	$('#btnClose'+time).live('click', function() {
		$('#MapAmap'+time).remove();
	});
	//保存操作
	$('#save'+time).live('click', function() {
		pointxy = $('#pointxy'+time).html();
		point = pointxy.split(',');
		$('#'+xpoint).val(point[0]);
		$('#'+ypoint).val(point[1]);
		$('#MapAmap'+time).remove();
	});
	//取消按钮
	$('#cancel'+time).live('click', function() {
		$('#MapAmap'+time).remove();
	});
	//最大化或还原窗口
	$('#btnFull'+time).live('click', function(e) {
		Y = $(this).scrollTop();
		if($('#MapAmap'+time).attr('ismax')=='true'){
			resize(500,400,Y);
			setCenter();
			$('#MapAmap'+time).attr('ismax','');
		}else{
			resize('','',Y);
			$('#MapAmap'+time).attr('ismax','true');
		}
	});
	//拖动窗口
	var flag=true;
	$('#main'+time).live('mousedown', function(e) {
		stopPropagation(e);
		$(this).css('cursor','move');
		offsetLeft=e.clientX+(document.documentElement.scrollLeft||document.body.scrollLeft)-parseInt(pannel.style.left);
		offsetTop=e.clientY+(document.documentElement.scrollTop||document.body.scrollTop)-parseInt(pannel.style.top);
		if(flag==true){
			flag=false;
			$(this).mousemove(function(e){
				drag(e);
			});
		}
		$(this).mouseup(function(e){
			$(this).css('cursor','default');
			flag=true;
			$(this).unbind('mousemove');
		});
	});
	function resize(w,h,Y){
		h = h?h:$(window).height();
		w = w?w:$(window).width();
		$('#MapAmap'+time).css({'width':w+'px','top':Y+'px','left':'0px',});
		$('#con'+time).css({'width':w+'px','height':h+'px',});
		$('#bo'+time).css({
			'width':(w-2)+'px',
			'height':(h-80)+'px',
		});
		$('#amapmap').css({
			'height':(h-90)+'px',
		});
		$('#main'+time).css({
			'width':(w-22)+'px',
		});
	}
	//window.onresize=setPosition;
	function setCenter(){
		var w=document.documentElement.offsetWidth||document.body.offsetWidth;
		var h=document.documentElement.clientHeight||document.body.clientHeight;
		var top=document.documentElement.scrollTop||document.body.scrollTop;
		var left=document.documentElement.scrollLeft||document.body.scrollLeft;
		$(pannel).css('position','absolute');
		$(pannel).css('z-index','9999');
		$(pannel).css('top',(h-pannel.offsetHeight)/2+top+"px");
		$(pannel).css('left',(w-pannel.offsetWidth)/2+left+"px");
	};
	function drag(e){
		stopPropagation(e);
		var x=e.clientX;
		var y=e.clientY;
		var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
		var scrollLeft=document.documentElement.scrollLeft||document.body.scrollLeft;
		pannel.style.left=x+scrollLeft-offsetLeft+"px";
		pannel.style.top=y+scrollTop-offsetTop+"px";
	};
	function pannelResize(e){
		stopPropagation(e);
		var left=e.clientX-startLeft;
		var top=e.clientY-startTop;
		var con=document.getElementById("con"+time);
		var main=document.getElementById("main"+time);
		var bo=document.getElementById("bo"+time);
		var mapC=document.getElementById("map"+time);
		var ifra=document.getElementById("ifra"+time);
		if(width+left<500||height+top<410){return;};
		con.style.width=(width+left)+"px";
		con.style.height=(height+top+8)+"px";
		main.style.width=(width+left-22)+"px";
		bo.style.height=(height+top-80)+"px";
		mapC.style.height=(height+top-90)+"px";		
		ifra.style.width=(width+left+1)+"px";
		ifra.style.height=(height+top-2)+"px";
		map.notifyResize();
	};
	function stopPropagation(e){
		if(e.stopPropagation){
			e.stopPropagation();
			e.preventDefault();
		}else{
			e.cancelBubble=true;
		}
	};
}
var amapmap;
function init_map (px,py,map_id,address_id) {
	var map_id     = map_id ? map_id : 'amapmap';
	var address_id = address_id ? address_id : 'address';
	loading('加载地图中...');
	jQuery.getScript("http://webapi.amap.com/maps?v=1.3&key=cdc352e3340dfd70478e4844e9572ae9", function(){
		default_px = px ? px : 39.90923;
		default_py = py ? py : 116.397428;
		amapmap = new AMap.Map(map_id,{
			rotateEnable:true,
			dragEnable:true,
			zoomEnable:true,
			zooms:[3,18],
			zoom:13 //地图显示的缩放级别
		});
		address = $('#' + address_id).val();
		if(!px && !py && address){
			geocoder_xy(address);
		}
		//在地图中添加ToolBar插件
		amapmap.plugin(["AMap.ToolBar"],function(){		
			toolBar = new AMap.ToolBar();
			amapmap.addControl(toolBar);
		});
		//将标注添加到地图中

		//为地图注册click事件获取鼠标点击出的经纬度坐标
		var clickEventListener = AMap.event.addListener(amapmap,'click',function(e){
			py = e.lnglat.getLng();
			px = e.lnglat.getLat();
			addMarker(px,py);
			if(address_id){
				xy2address(px, py, address_id);
			}
		});
		removeLoading();
		$('#' + map_id).append('<style>.amap-toolbar, .amap-scalecontrol, .amap-overviewcontrol{z-index:100;}</style>');
		addMarker(default_px,default_py);
	});
}
function amapMap(px,py) {
	loading('加载地图中...');
	jQuery.getScript("http://webapi.amap.com/maps?v=1.2&key=cdc352e3340dfd70478e4844e9572ae9", function(){
		default_px = px?px:39.90923;
		default_py = py?py:116.397428;
		amapmap = new AMap.Map("amapmap",{
			rotateEnable:true,
			dragEnable:true,
			zoomEnable:true,
			zooms:[3,18],
			zoom:13 //地图显示的缩放级别
		});
		address = $('.address').val();
		address = address ? address : $('.location').val();
		if(!px && !py && address){
			geocoder_xy(address);
		}
		//在地图中添加ToolBar插件
		amapmap.plugin(["AMap.ToolBar"],function(){		
			toolBar = new AMap.ToolBar();
			amapmap.addControl(toolBar);
		});
		//将标注添加到地图中
		addMarker(default_px,default_py);

		//为地图注册click事件获取鼠标点击出的经纬度坐标
		var clickEventListener = AMap.event.addListener(amapmap,'click',function(e){
			py = e.lnglat.getLng();
			px = e.lnglat.getLat();
			$('#pointxy'+time).html(py+','+px);
			addMarker(px,py);
		});

		removeLoading();
	});
}
//将标注添加到地图中
function addMarker(px,py){
	//清空所有覆盖物
	amapmap.clearMap();
	var marker = new AMap.Marker({				  
		icon:"http://webapi.amap.com/images/marker_sprite.png",
		position:new AMap.LngLat(py,px)
	});

	marker.setMap(amapmap);  //在地图上添加点
	amapmap.setFitView(); //调整到合理视野
	$('#point_x').val(py);
	$('#point_y').val(px);
}
//地理编码
function geocoder_xy(address) {
    var MGeocoder;
    //加载地理编码插件
    AMap.service(["AMap.Geocoder"], function() {        
        MGeocoder = new AMap.Geocoder({ 
            radius:1000 //范围，默认：500
        });
        //返回地理编码结果  
        //地理编码
        MGeocoder.getLocation(address, function(status, result){
        	if(status === 'complete' && result.info === 'OK'){
        		var geocode = new Array();
				var d = result.geocodes[0];
				var lngX = d.location.getLng();
				var latY = d.location.getLat();
				$('#pointxy'+time).html(lngX+','+latY);
				addMarker(latY,lngX);
        	}
        });
    });
}
function geocoder2xy (address_id) {
	var MGeocoder;
	address = $('#' + address_id).val();
    //加载地理编码插件
    AMap.service(["AMap.Geocoder"], function() {        
        MGeocoder = new AMap.Geocoder({ 
            radius:1000 //范围，默认：500
        });
        //返回地理编码结果  
        //地理编码
        MGeocoder.getLocation(address, function(status, result){
        	if(status === 'complete' && result.info === 'OK'){
        		var geocode = new Array();
				var d = result.geocodes[0];
				var lngX = d.location.getLng();
				var latY = d.location.getLat();
				addMarker(latY,lngX);
        	}
        });
    });
}
function xy2address (px, py, address_id) {
	var geocoder; 
	var lnglatXY = new AMap.LngLat(py,px);//需转为地址描述的坐标
	//加载地理编码插件 
	AMap.service(["AMap.Geocoder"], function() {       
	    geocoder = new AMap.Geocoder({
	        radius: 1000,
	        extensions: "all"
	    });
	    //逆地理编码
	    geocoder.getAddress(lnglatXY, function(status, result){
	        //取回逆地理编码结果
	        if(status === 'complete' && result.info === 'OK'){
	            address = result.regeocode.formattedAddress;
	            $('#' + address_id).text(address);
	            $('#' + address_id).val(address);
	        }
	    });
	});
}
